import pymysql


# 身份验证
def match_identity(username, password):
    # 建立数据库连接
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        database='food_price'
    )

    try:
        # 创建游标对象
        with connection.cursor() as cursor:
            # 执行SQL查询
            sql = "SELECT * FROM user WHERE account=%s AND password=%s"
            cursor.execute(sql, (username, password))

            # 获取查询结果
            result = cursor.fetchone()

            if result:
                # 身份匹配成功
                return True
            else:
                # 身份匹配失败
                return False
    finally:
        # 关闭数据库连接
        connection.close()


import pymysql


def register_user(username, password):
    # 建立数据库连接
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        database='food_price'
    )

    try:
        # 创建游标对象
        with connection.cursor() as cursor:
            # 检查用户名是否已存在
            check_sql = "SELECT * FROM user WHERE account=%s"
            cursor.execute(check_sql, (username,))
            existing_user = cursor.fetchone()

            if existing_user:
                # 用户名已存在
                return False

            # 注册新用户
            register_sql = "INSERT INTO user (account, password) VALUES (%s, %s)"
            cursor.execute(register_sql, (username, password))

            # 提交事务
            connection.commit()

            # 注册成功
            return True
    finally:
        # 关闭数据库连接
        connection.close()
